package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.project.bean.GradeBean;
import com.project.bean.RoleBean;
import com.project.service.IGradeService;
import com.project.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("role")
@Api(tags = "角色")
public class RoleController {
    @Autowired
    private IRoleService roleService;


    @ApiOperation(value = "查询所有角色信息" )
    @GetMapping("findAll")
    public List<RoleBean> findAll() {
        return roleService.findAll();
    }

    @ApiOperation(value = "分页查询所有角色信息" )
    @GetMapping("cutAll")
    public IPage<RoleBean> cutAll(Integer pageNO) {
        return roleService.cutAll(pageNO);
    }

    @ApiOperation(value = "添加角色" )
    @PostMapping("add")
    public String add(@RequestBody Map<String, Object> params) {
        String roleName = (String) params.get("roleName");
        String gradeIdArrayJson = (String) params.get("gradeIdArray");
        ObjectMapper mapper = new ObjectMapper();
        List<Integer> gradeIdList = null;
        try {
            gradeIdList = mapper.readValue(gradeIdArrayJson, new TypeReference<List<Integer>>() {});
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        roleService.add(roleName, gradeIdList.toArray(new Integer[0]));
        return "ok";
    }

    @ApiOperation(value = "删除角色" )
    @DeleteMapping("del")
    public String del(Integer roleId) {
        roleService.del(roleId);
        return "ok";
    }

    @ApiOperation(value = "查看角色" )
    @GetMapping("findById")
    public RoleBean findById(Integer roleId) {
      return roleService.findById(roleId);
    }

    @ApiOperation(value = "修改角色权限" )
    @GetMapping("updateGrade")
    public String updateGrade(Integer roleId, Integer[] gradeIdArray) {
        roleService.updateGrade(roleId,gradeIdArray);
        return "ok";
    }
//    public String updateGrade(@RequestBody Map<Integer, Object> params) {
//        Integer roleId = (Integer) params.get("roleId");
//        String gradeIdArrayJson = (String) params.get("gradeIdArray");
//        ObjectMapper mapper = new ObjectMapper();
//        List<Integer> gradeIdList = null;
//        try {
//            gradeIdList = mapper.readValue(gradeIdArrayJson, new TypeReference<List<Integer>>() {});
//        } catch (JsonProcessingException e) {
//            e.printStackTrace();
//        }
//        roleService.updateGrade(roleId,gradeIdList.toArray(new Integer[0]));
//        return "ok";
//    }

}
